package com.play.ballen.synthetic.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.play.ballen.R
import com.yuven.baselib.utils.ResourceUtil
import org.apachex.commons.codec.binary.StringUtils

class NoticeDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var tvNotice: TextView
    private lateinit var button: Button

    var numberCallback: ((Int) -> Unit)? = null


    fun show(status:Int) {
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_notice, null)
            contentView.let {
                tvNotice = it.findViewById(R.id.tv_notice)
                button = it.findViewById(R.id.btnOk)
                if(status==1){
                    tvNotice.text = ResourceUtil.getString(R.string.success_hecheng)
                    button.text = ResourceUtil.getString(R.string.to_receive)
                }else{
                    tvNotice.text = ResourceUtil.getString(R.string.failure_hecheng)
                    button.text = ResourceUtil.getString(R.string.thanks_participation)
                }
                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener { btn ->
                    dialog.dismiss()
                }
                it.findViewById<TextView>(R.id.btnOk).setOnClickListener { btn ->
//                    if (edNum.text.toString().isEmpty()) {
//                        ToastUtils.show(edNum.hint.toString())
//                        return@setOnClickListener
//                    }
//                    val num = edNum.text.toString().toInt()
//                    if (num == 0) {
//                        ToastUtils.show(edNum.hint.toString())
//                        return@setOnClickListener
//                    }
                    dialog.dismiss()
                    numberCallback?.invoke(0)
                }

                dialog.setContentView(it)
                dialog.setOnDismissListener { }
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
                dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
            }
        }

        if (!dialog.isShowing) {
            dialog.show()
        }
    }

}